package jp.co.nes.awssupport.common.utility;

import java.util.UUID;
import java.util.concurrent.Future;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.batch.AWSBatchAsync;
import com.amazonaws.services.batch.AWSBatchAsyncClientBuilder;
import com.amazonaws.services.batch.model.SubmitJobRequest;
import com.amazonaws.services.batch.model.SubmitJobResult;

/**
 * AwsBatchAsyncヘルパーです。<br>
 * AWS Batchに関する機能を提供します。
 */
public class AwsBatchAsyncHelper extends AwsHelperBase {

    /** クライアント */
    private AWSBatchAsync client;

    /**
     * デフォルトコンストラクタです。
     */
    public AwsBatchAsyncHelper() {
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.utility.AwsHelperBase#init(com.amazonaws.regions.Regions, com.amazonaws.auth.AWSCredentials, com.amazonaws.ClientConfiguration)
     */
    @Override
    protected void init(Regions regions, AWSCredentials credentials, ClientConfiguration config) {
        boolean isLocalTest = Boolean.valueOf(PropertiesUtil.getProperty("LocalTest", "false"));
        if (!isLocalTest) {
            if (credentials == null) {
                if (config == null) {
                    client = AWSBatchAsyncClientBuilder.standard()
                            .withRegion(regions)
                            .build();
                } else {
                    client = AWSBatchAsyncClientBuilder.standard()
                            .withRegion(regions)
                            .withClientConfiguration(config)
                            .build();
                }
            } else {
                if (config == null) {
                    client = AWSBatchAsyncClientBuilder.standard()
                            .withRegion(regions)
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(new ClientConfiguration())
                            .build();
                } else {
                    client = AWSBatchAsyncClientBuilder.standard()
                            .withRegion(regions)
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(config)
                            .build();
                }
            }
        }
    }

    /**
     * ジョブを非同期で投入します。
     *
     * @param jobNamePrefix ジョブ名のプリフィックス
     * @param jobQueue ジョブキュー
     * @param jobDefinition ジョブ定義
     * @return 投入に関する結果
     */
    public Future<SubmitJobResult> submitJobAsync(String jobNamePrefix, String jobQueue, String jobDefinition) {
        SubmitJobRequest request = new SubmitJobRequest();
        request.setJobName(jobNamePrefix + "-" + UUID.randomUUID().toString());
        request.setJobQueue(jobQueue);
        request.setJobDefinition(jobDefinition);
        return client.submitJobAsync(request);
    }

    /**
     * クライアントをシャットダウンし、リソースを解放します。<br>
     * 詳細は{@link AWSBatchAsync#shutdown()}を参照してください。
     *
     * @see AWSBatchAsync#shutdown()
     */
    public void shutdown() {
        client.shutdown();
    }
}
